module("luci.controller.mt5700webui", package.seeall)

local i18n = require "luci.i18n"
local _ = i18n.translate
local uci = require "luci.model.uci".cursor()
local sys = require "luci.sys"

function index()
    entry({"admin", "services", "mt5700webui"}, alias("admin", "services", "mt5700webui", "panel"), _("MT5700 WebUI"), 60)
    entry({"admin", "services", "mt5700webui", "panel"}, call("show_panel"), _("Web Panel"), 10)
    entry({"admin", "services", "mt5700webui", "config"}, cbi("mt5700webui"), _("Configuration"), 20)
    entry({"admin", "services", "mt5700webui", "open"}, call("open_webui"), nil, 30)
    entry({"admin", "services", "mt5700webui", "config", "start"}, call("start_service"), nil, 40)
    entry({"admin", "services", "mt5700webui", "config", "stop"}, call("stop_service"), nil, 40)
    entry({"admin", "services", "mt5700webui", "config", "restart"}, call("restart_service"), nil, 40)
    entry({"admin", "services", "mt5700webui", "status"}, call("get_status"), nil, 50)
end

function show_panel()
    local port = uci:get("mt5700webui", "main", "port") or "8002"
    local running = sys.call("/etc/init.d/mt5700webui status >/dev/null 2>&1") == 0
    local ip = luci.http.getenv("SERVER_ADDR")
    
    if ip:match(":") then
        ip = "["..ip.."]"
    end
    
    local url = "http://" .. ip .. ":" .. port
    
    luci.template.render("mt5700webui_panel", {
        title = _("MT5700 WebUI Panel"),
        url = url,
        running = running
    })
end

function open_webui()
    local port = uci:get("mt5700webui", "main", "port") or "8002"
    local ip = luci.http.getenv("SERVER_ADDR")
    
    if ip:match(":") then
        ip = "["..ip.."]"
    end
    
    local url = "http://" .. ip .. ":" .. port
    
    luci.http.redirect(url)
end

function start_service()
    sys.call("chmod +x /etc/init.d/mt5700webui")
    sys.call("/etc/init.d/mt5700webui start >/dev/null 2>&1")
    luci.http.redirect(luci.dispatcher.build_url("admin/services/mt5700webui/panel"))
end

function stop_service()
    sys.call("/etc/init.d/mt5700webui stop >/dev/null 2>&1")
    luci.http.redirect(luci.dispatcher.build_url("admin/services/mt5700webui/panel"))
end

function restart_service()
    sys.call("/etc/init.d/mt5700webui restart >/dev/null 2>&1")
    luci.http.redirect(luci.dispatcher.build_url("admin/services/mt5700webui/panel"))
end

function get_status()
    local running = sys.call("/etc/init.d/mt5700webui status >/dev/null 2>&1") == 0
    
    luci.http.prepare_content("application/json")
    luci.http.write_json({
        running = running
    })
end